﻿/*
 * Almathy Center - MouseHook.cs
 * Copyright (C) 2011 Christophe LEMOINE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * */
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace AlmathyCenter.Datas
{

    public class MouseHook
    {

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, MouseProcedure lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        private const int WH_MOUSE_LL = 14;

        private IntPtr SetHook(MouseProcedure proc)
        {
            using (Process currentProcess = Process.GetCurrentProcess())
                using (ProcessModule currentModule = currentProcess.MainModule)
                    return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(currentModule.ModuleName), 0);
        }
 
        private delegate IntPtr MouseProcedure(int nCode, IntPtr wParam, IntPtr lParam);

        public event EventHandler MouseMoveDetected;

        private void OnMouseMoveDetected(object sender, EventArgs args)
        {
            if (MouseMoveDetected != null)
                MouseMoveDetected(sender, args);
        }
 
        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
                OnMouseMoveDetected(null, new EventArgs());

            return CallNextHookEx(mouseHookId, nCode, wParam, lParam);
        }

        private MouseProcedure mouseProcedure;
        private MouseProcedure CurrentMouseProcedure
        {
            get
            {
                if (mouseProcedure == null)
                {
                    mouseProcedure = HookCallback;
                }
                return mouseProcedure;
            }
            set { mouseProcedure = value; }
        }

        private IntPtr mouseHookId = IntPtr.Zero;

        public MouseHook()
        {
            mouseHookId = SetHook(CurrentMouseProcedure);
        }

        public void Dispose()
        {
            UnhookWindowsHookEx(mouseHookId);
        }

    }

}
